/**
 * Created by machan on 2017/4/1.
 */

import * as constants from '../common/Constants.js';
import * as umfUtils from '../utils/UmfUtils.js';
import UmfLog from '../utils/UmfLog';

const AgentLevels = {
    FIRST: "1",
    SECOND: "2",
    THIRD: "3"
};

const UserTypes = {
    AGENT: "1",
    SALEMAN: "2",
};

// const PermVals = {
//     HAS_PERM: "1",
//     NO_PERM: "0",
// };

const PermValues = {
    SEARCHPROFITPERM: "searchProfitPerm",
    SEARCHSTOREPERM: "searchStorePerm",
    FETCHBILLDOCPERM: "fetchBillDocPerm"
}

function userInfo() {
    this.assistId = '';
    this.agentId = '';
    this.agentName = '';
    this.userId = '';
    this.userType = '';
    this.productAttribute = '';

    this.agentLevel = '';
    this.permissionConf = '';
    this.permConfig = {};

    this.oriPwdChange = false;

    /**
     * 获取用户数据
     * @returns {userInfo}
     */
    this.get = ()=> {
        let {assistId, agentId, agentName, userId, userType, productAttribute, agentLevel, permissionConf, oriPwdChange} = _getUserInfo();

        this.assistId = assistId;
        this.agentId = agentId;
        this.agentName = agentName;
        this.userId = userId;
        this.userType = userType;
        this.productAttribute = productAttribute;

        this.agentLevel = agentLevel;
        this.permissionConf = permissionConf;
        this.permConfig = new permConfig(this.permissionConf, this.userType, this.agentLevel);

        this.oriPwdChange = oriPwdChange;
        return this;
    };

    /**
     * 查询用户数据完整性
     * @returns {boolean}
     */
    this.checkUserData = ()=> {
        return !!(this.assistId
        && this.agentId
        && this.agentName
        && this.userId
        && this.userType
        && this.productAttribute
        && this.agentLevel
        //是否修改过初始密码
        && this.oriPwdChange);
    };

    this.isAgent = ()=> {
        return UserTypes.AGENT === this.userType;
    }
}

/**
 * 权限配置
 * @param userType
 * @param agentLevel
 * @param conf
 */
function permConfig(conf, userType, agentLevel) {
    this.searchProfitPerm = false;
    this.searchStorePerm = false;
    this.fetchBillDocPerm = false;
    if (conf) {
        this.searchProfitPerm = conf.indexOf(PermValues.SEARCHPROFITPERM) >= 0;
        this.searchStorePerm = conf.indexOf(PermValues.SEARCHSTOREPERM) >= 0;
        this.fetchBillDocPerm = conf.indexOf(PermValues.FETCHBILLDOCPERM) >= 0;
    }

    /**
     * 代理商商户查询权限
     * @returns {boolean}
     */
    this.canSearchStores = ()=> {
        if (AgentLevels.FIRST === agentLevel ||
            AgentLevels.SECOND === agentLevel) {
            if (UserTypes.AGENT === userType) {
                return true;
            } else {
                return this.searchStorePerm;
            }
        }
        return false;
    };

    /**
     * 分润查询权限
     * @returns {boolean}
     */
    this.canSearchProfit = ()=> {
        if (AgentLevels.FIRST === agentLevel) {
            if (UserTypes.AGENT === userType) {
                return true;
            } else {
                return this.searchProfitPerm
            }
        }
        return false;
    };

    /**
     * 获取对账文件权限
     * @returns {boolean}
     */
    this.canFetchBillDoc = () => {
        if (UserTypes.AGENT === userType) {
            return true;
        } else {
            return this.fetchBillDocPerm;
        }
    };

    /**
     * 是否有代理商管理
     * @returns {boolean}
     */
    this.canManageAgent = ()=> {
        if (UserTypes.AGENT === userType && AgentLevels.THIRD !== agentLevel) {
            return true;
        }
        return false;
    };

    /**
     * 获取权限配置数组
     * @param searchProfitPerm
     * @param searchStorePerm
     * @param fetchBillDocPerm
     * @returns {Array}
     */
    this.getConfArray = (searchProfitPerm, searchStorePerm, fetchBillDocPerm)=> {
        let confArr = [];
        searchProfitPerm && confArr.push(PermValues.SEARCHPROFITPERM);
        searchStorePerm && confArr.push(PermValues.SEARCHSTOREPERM);
        fetchBillDocPerm && confArr.push(PermValues.FETCHBILLDOCPERM);
        return confArr;
    }
}

function saveUserInfo(userInfo){
    UmfLog.log("saveUserInfo: ");
    UmfLog.log(userInfo);
    if (userInfo) {
        let userData =  {
            assistId: userInfo.assistId,
            agentId: userInfo.agentId,
            agentName: userInfo.agentName,
            userId: userInfo.userId,
            userType: userInfo.userType,
            productAttribute: userInfo.productAttribute,

            agentLevel: userInfo.agentLevel,
            permissionConf: userInfo.permissionConf,

            oriPwdChange: userInfo.oriPwdChange
        };


        try {
            wx.setStorageSync(constants.Keys.USER_INFO, userData);
        } catch (e) {
            UmfLog.error("saveUserInfo error!");
        }
    }
}

function _getUserInfo() {
    return umfUtils.getStorageSync(constants.Keys.USER_INFO);
}

module.exports = {
    userInfo: userInfo,
    saveUserInfo: saveUserInfo,
    permConfig: permConfig,
};